package com.ricchieri.manage.bean;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import com.ricchieri.common.BusinessSettings;
import com.ricchieri.filter.EmailTemplateFilter;
import com.ricchieri.lazymodel.EmailTemplateLazyDataModel;
import com.ricchieri.model.EmailTemplate;
import com.ricchieri.model.EmailToSendType;
import com.ricchieri.service.interf.IEmailTemplateService;
import com.ricchieri.service.interf.IEmailToSendTypeService;
import com.ricchieri.util.FacesUtil;

@ManagedBean(name = "searchEmailTemplateMB")
@ViewScoped
public class SearchEmailTemplateManageBean extends GenericManageBean {

	private static final long serialVersionUID = -275710803306101330L;
	
	@ManagedProperty(value = "#{EmailToSendTypeService}")
	IEmailToSendTypeService emailTypeService;
	
	@ManagedProperty(value = "#{EmailTemplateService}")
	IEmailTemplateService emailTemplateService;
	
	private EmailTemplateFilter emailTemplateFilter;
	private Map<String,String> templateType = null;
	private EmailTemplate selectedEmailTemplate;
	private EmailTemplateLazyDataModel emailTemplateLazyDataModel;
	
	@PostConstruct
	public void init() {
		emailTemplateFilter = (EmailTemplateFilter) FacesUtil.getParametersFlash(BusinessSettings.KEY_FILTER);
		
		if(emailTemplateFilter == null) {
			emailTemplateFilter = new EmailTemplateFilter();
		}
	}	
	
	public Map<String, String> getTemplateType() {
		if (templateType == null){
			templateType = new TreeMap<String, String>();
			List<EmailToSendType> template = emailTypeService.getAllEmailToSendType();
			for (EmailToSendType tem : template) {
				templateType.put(FacesUtil.getMessage(tem.getDescription()), String.valueOf(tem.getId()));
			}
		}
		return templateType;
	}

	public IEmailToSendTypeService getEmailTypeService() {
		return emailTypeService;
	}

	public void setEmailTypeService(IEmailToSendTypeService emailTypeService) {
		this.emailTypeService = emailTypeService;
	}

	public IEmailTemplateService getEmailTemplateService() {
		return emailTemplateService;
	}

	public void setEmailTemplateService(IEmailTemplateService emailTemplateService) {
		this.emailTemplateService = emailTemplateService;
	}

	public EmailTemplateFilter getEmailTemplateFilter() {
		return emailTemplateFilter;
	}

	public void setEmailTemplateFilter(EmailTemplateFilter emailTemplateFilter) {
		this.emailTemplateFilter = emailTemplateFilter;
	}

	public void setTemplateType(Map<String, String> templateType) {
		this.templateType = templateType;
	}


	public void search() {
		emailTemplateLazyDataModel = null;
	}
	
	public String viewEmailTemplate(EmailTemplate template) {
		FacesUtil.addParametersFlash(BusinessSettings.KEY_EMAIL_TEMPLATE, template);
		FacesUtil.addParametersFlash(BusinessSettings.KEY_FILTER, emailTemplateFilter);
		
		return BusinessSettings.VIEW_DETAILS;
	}
	
	public String editEmailTemplate(EmailTemplate template) {
		FacesUtil.addParametersFlash(BusinessSettings.KEY_EMAIL_TEMPLATE, template);
		FacesUtil.addParametersFlash(BusinessSettings.KEY_FILTER, emailTemplateFilter);
		
		return BusinessSettings.EDIT;
	}
	
	public void reset() {
		emailTemplateFilter = new EmailTemplateFilter();
		emailTemplateLazyDataModel = null;
	}
	
	public void showDialog(EmailTemplate selectedEmailTemplate) {
		this.selectedEmailTemplate = selectedEmailTemplate;
	}
	
	public void delete() {
		getEmailTemplateService().deleteEmailTemplate(getSelectedEmailTemplate());
		getEmailTemplateLazyDataModel().setRowCount(getEmailTemplateLazyDataModel().getRowCount() -1 );
		FacesUtil.showInfoMessage("searchEmailTemplateForm", "emailTemplate_deleted");
	}
	
	public void unselect() {
		this.setSelectedEmailTemplate(null);
	}

	public EmailTemplate getSelectedEmailTemplate() {
		return selectedEmailTemplate;
	}

	public void setSelectedEmailTemplate(EmailTemplate selectedEmailTemplate) {
		this.selectedEmailTemplate = selectedEmailTemplate;
	}

	public EmailTemplateLazyDataModel getEmailTemplateLazyDataModel() {
		if(emailTemplateLazyDataModel == null) {
			emailTemplateLazyDataModel = new EmailTemplateLazyDataModel(getEmailTemplateService(), getEmailTemplateFilter());
		}
		return emailTemplateLazyDataModel;
	}

	public void setEmailTemplateLazyDataModel(
			EmailTemplateLazyDataModel emailTemplateLazyDataModel) {
		this.emailTemplateLazyDataModel = emailTemplateLazyDataModel;
	}
	
	

}

